function [comp]=RAICAR1D(dire,n1,n2,n3)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%R.Magalhaes, Paris, July 2019
%Function to align the components of all the different
%permutations as described in Yang et al (2007)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%This version uses the first permutation as the reference,
%doing RAICAR along one dimension.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Must choose the range of values to build correlation matrix
%and step size (e.g between 5 and 400 permutations in steps of 5
%Usage: RAICARD1D('directory',min number of permutations, size step,max number of permutations);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Loaded file can be created with matlab function CRCM1D made available
%within the same set of functions
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%This software is provided under a Free for Non-Commercial Use Only Agreement and a Non-Commercial use agreement
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%contact: ricardo.magalhaes@cea.fr

load(strcat(dire,'/CRCM_mat1D400'),'set','sizes','index','crcm');
setall=set;sizesall=sizes;indexall=index;crcmall=crcm;
clear set, sizes, index, crcm;
for n=n1:n2:n3
set=setall(1:n,1:n);
sizes=sizesall(1:n);
index=indexall(:,1:n);
crcm=crcmall(1:sum(sizes),1:sum(sizes));
%load(strcat(dire,'/CRCM_mat',int2str(n)),'set','sizes','index','crcm');

c=1;
while (c<64)

k=1;

for j=1:size(set,2)
size(set(1,j).mat)
if(size(set(1,j).mat,2)>0)
[maxes(k,1) maxes(k,2)]=max(set(1,j).mat(:)); %and save the value and position in matrix
end
k=k+1;
end

[M I]=max(squeeze(maxes(:,1))); %find the maximum of maximuns
[X Y]=ind2sub([1 size(set,2)],I); %get the set x y index for the maximum
[x y]=ind2sub(size(set(X,Y).mat),maxes(I,2)); %get the x y indexes for the maximum position within the mat matrix for set X Y
comp(c).r(1,:)=[set(X,Y).indexx(x),set(X,Y).indexy(y),M,Y]; %add the global maximum as the reference saving the components indexes, the correlation value and the set indexes
set(1,Y).mat(x,:)=[];set(1,Y).indexx(x)=[]; %eliminate the line matching the maximum and its index denominator
%set(1,Y).mat(:,y)=[];set(1,Y).indexy(y)=[]; %the same for the collumn

k=2;
for i=1:length(set) %search through all the sets for the components that best match this first one
if(size(set(1,i).mat,2)>0)
if(i~=Y)
if(i==1) %if one of the indexes puts us over the diagonal we will remove a line and a collumn, to keep sizes balanced. This will only happen once per iteration
set(1,1).mat(x,:)=[];set(1,1).indexx(x)=[];%set(1,1).mat(:,y)=[];set(1,1).indexy(y)=[];
else %if we are not on the diagonal we will test for set i, if its better correlated with x or y
[m1,i1]=max(squeeze(set(1,i).mat(x,:)));
comp(c).r(k,:)=[comp(c).r(1,1),set(X,i).indexy(i1),m1,i];k=k+1; %save the components indexes matching the maximum, the correlations andindexes info

set(X,i).mat(x,:)=[];set(X,i).indexx(x)=[]; %eliminate the lines and collums of the initial chosen component
%set(X,i).mat(:,i1)=[];set(X,i).indexy(i1)=[];
end
end
end
end
c=c+1;
maxes=[];
end

save(strcat(dire,'/RAICAR1Dresultsnew',int2str(n)),'comp');
end
end